<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="公司名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="状态" clearable>
          <el-option
            v-for="dict in sys_company_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container">
      <el-row :gutter="10" class="mb-10px">
        <el-col v-if="dataPermissions == 1" :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd()"
            v-hasPermi="['company:company:add']"
          >新增</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="companyList" row-key="id">
        <el-table-column label="公司名称" prop="name" />
        <el-table-column label="公司负责人" align="center" prop="head" />
        <el-table-column label="联系电话" align="center" prop="phone" />
        <el-table-column label="公司地址" align="center" prop="address" show-overflow-tooltip />
        <el-table-column label="状态" align="center" width="100">
          <template #default="scope">
            <el-switch
              v-model="scope.row.state"
              :active-value="1"
              :inactive-value="2"
              inline-prompt
              active-text="启用"
              inactive-text="禁用"
              :disabled="scope.row.check == false"
              @change="handleStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="update_time" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.update_time, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
          <template #default="scope">
            <template v-if="scope.row.check != false">
              <el-tooltip content="详情" placement="top">
                <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['company:company:query']"></el-button>
              </el-tooltip>
            </template>
            <template v-if="(scope.row.lev == 1 && scope.row.check) || scope.row.lev == 2">
              <el-tooltip content="修改" placement="top">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['company:company:edit']"></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['company:company:remove']"></el-button>
              </el-tooltip>
            </template>
            <template v-if="scope.row.lev == 1 && scope.row.state == 1 && scope.row.check">
              <el-tooltip content="添加子项" placement="top">
                <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row.id)" v-hasPermi="['company:company:add']"></el-button>
              </el-tooltip>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司对话框 -->
    <Dialog v-model="showDialog" :title="dialogTitle" width="60%">
      <edit-company
        v-if="dialogType == 1" 
        ref="editCompany"
        @cancel="cancel"
        @confirm="confirm"
      />
      <company-detail 
        v-if="dialogType == 2"
        ref="companyDetail"
        @cancel="cancel"
      />
    </Dialog>
  </div>
</template>

<script setup name="Company">
import { listCompany, delCompany, updateCompany, getAllCompanies } from "@/api/company/company"
import { nextTick } from "vue"
import EditCompany from './edit.vue'
import CompanyDetail from './detail.vue'
import useUserStore from '@/store/modules/user'
import { setToken } from '@/utils/auth'

const editCompany = ref()
const companyDetail = ref()
const { proxy } = getCurrentInstance()
const { sys_company_status } = proxy.useDict("sys_company_status")

const companyList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const total = ref(0)
const showDialog = ref(false)
const dialogTitle = ref('')
const dialogType = ref(0)               // 1新增修改  2详情
const dataPermissions = ref()           // 1全部数据权限  2自定义数据权限

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    state: null
  }
})

const { queryParams } = toRefs(data)

/** 查询公司列表 */
function getList() {
  loading.value = true
  getAllCompanies(queryParams.value).then(res => {
    companyList.value = res.data || []
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
  dialogTitle.value = ''
  dialogType.value = 0
}

// 确认按钮
const confirm = () => {
  cancel()
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 新增按钮操作 */
function handleAdd(id) {
  showDialog.value = true
  dialogTitle.value = '添加公司'
  dialogType.value = 1
  nextTick(() => {
    editCompany.value.pageInit('', id)
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  showDialog.value = true
  dialogTitle.value = '修改公司'
  dialogType.value = 1
  nextTick(() => {
    editCompany.value.pageInit(_id)
  })
}

/** 公司详情 */
function handleDetail(row) {
  dialogTitle.value = '公司详情'
  dialogType.value = 2
  showDialog.value = true
  nextTick(() => {
    companyDetail.value.pageInit(row.id)
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除该公司？').then(function() {
    delCompany(_ids).then((res) => {
      proxy.$modal.msgSuccess("删除成功")
      setToken(res.msg)
      getList()
    })
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('company/company/export', {
    ...queryParams.value
  }, `公司列表_${proxy.getFileImportName()}.xlsx`)
}

/** 状态修改 */
function handleStatusChange(row) {
  let text = row.state == 1 ? "启用" : "禁用"
  proxy.$modal.confirm('确认要"' + text + '""' + row.name + '"公司吗?').then(function () {
    const params = {
      id: row.id,
      state: row.state,
      name: row.name
    }
    return updateCompany(params)
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功")
  }).catch(function () {
    row.state = row.state == 1 ? 2 : 1
  })
}

dataPermissions.value = useUserStore().dataPermissions
getList()
</script>
